package fuse.LaCollaFS;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;

import LaColla.Api.Api;
import LaColla.core.data.TimestampSummary;
import LaColla.core.util.Debug;
import fuse.LaCollaFS.Api.ApiImpl;
import fuse.LaCollaFS.Api.ApiServer;
import fuse.LaCollaFS.Metadades.MetaDades;

import junit.framework.TestCase;

public class testOrganitzador extends TestCase {

	private Organitzador org;

	//private static MetaDades dades;

	private static final int RMIport = 11099;

	private static String appIP;

	private static int apiPort = 19000;

	private static int appPort = 4321;

	private static String username = "admin";

	private static String passwd = "admin";

	private static String groupId = "hola";

	private Api api;

	private ApiServer aserv;

	private String memberId;

	private String apiid;


	private void p(String string) {
		System.out.println(string);

	}



	public void testStat() {
		try {
			appIP = InetAddress.getLocalHost().getCanonicalHostName();
		} catch (UnknownHostException e1) {

			e1.printStackTrace();
		}
		// Debug.say("hola LaCollaFS", appIP);
		p("LaCollaFS : Host= " + appIP + " appPort=" + appPort
				+ " LaCOLLAport= " + apiPort);

		aserv = new ApiServer(appIP, appPort);
		// p("Api Server creat..." + aserv);

		api = aserv.resolveApi(apiPort);
		TimestampSummary s;
		s = aserv.readSummary();
		// p("tss:"+s);

		String gapaIP = appIP;
		int gapaPort = 2001;
		try {
			apiid = api.login(groupId, username, passwd, gapaIP, gapaPort, appIP,
					appPort, aserv.readSummary());
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// en aquest punt encara pot no haber arribat el
		// newConnectedMemberEvent, s'hauria de bloquejar..
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		} // esperem rebre l'event
		memberId = ((ApiImpl) aserv.getASAI()).getMemberId();
		p("Membre conectat, el meu id és : " + memberId);
		p("Numero de nodes presents: "+aserv.getNumMembers());
	//	dades = new MetaDades(defpath, api, aserv, apiid);
		org = new Organitzador(api, aserv, apiid);
		assertTrue(true);

		
		
		assertTrue(true);
		int i = org.CanviarStat("/hola");
		assertTrue(i>0);
		org.ObtenirStat("/hola");
		int j = org.CanviarStat("/hola");

		assertEquals(j,-1);
		org.Fet(i);
		 j = org.CanviarStat("/hola");
		 org.Fet(j);
		 
		 j = org.CanviarStat("/hola");
		 org.Fet(j);
		assertTrue(j>0);
		//p("TEST: valor j "+j);
		

	
		
		try {
			api.logout(groupId, memberId, apiid);
			aserv.unpublish();
			aserv.close();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
	
	public void testFitxer(){
		
		try {
			appIP = InetAddress.getLocalHost().getCanonicalHostName();
		} catch (UnknownHostException e1) {

			e1.printStackTrace();
		}
		// Debug.say("hola LaCollaFS", appIP);
		p("LaCollaFS : Host= " + appIP + " appPort=" + appPort
				+ " LaCOLLAport= " + apiPort);

		aserv = new ApiServer(appIP, appPort);
		// p("Api Server creat..." + aserv);

		api = aserv.resolveApi(apiPort);
		TimestampSummary s;
		s = aserv.readSummary();
		// p("tss:"+s);

		String gapaIP = appIP;
		int gapaPort = 2001;
		try {
			apiid = api.login(groupId, username, passwd, gapaIP, gapaPort, appIP,
					appPort, aserv.readSummary());
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// en aquest punt encara pot no haber arribat el
		// newConnectedMemberEvent, s'hauria de bloquejar..
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		} // esperem rebre l'event
		memberId = ((ApiImpl) aserv.getASAI()).getMemberId();
		p("Membre conectat, el meu id és : " + memberId);
		p("Numero de nodes presents: "+aserv.getNumMembers());
	//	dades = new MetaDades(defpath, api, aserv, apiid);
		org = new Organitzador(api, aserv, apiid);
		assertTrue(true);

		
		
		
		int i = org.EscriureFitxer("/hola.txt");
		int j = org.LlegirFitxer("/hola.txt");
		
		org.FitxerDesat("/hola.txt");

		try {
			api.logout(groupId, memberId, apiid);
			aserv.unpublish();
			aserv.close();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void testDir(){
		
		try {
			appIP = InetAddress.getLocalHost().getCanonicalHostName();
		} catch (UnknownHostException e1) {

			e1.printStackTrace();
		}
		// Debug.say("hola LaCollaFS", appIP);
		p("LaCollaFS : Host= " + appIP + " appPort=" + appPort
				+ " LaCOLLAport= " + apiPort);

		aserv = new ApiServer(appIP, appPort);
		// p("Api Server creat..." + aserv);

		api = aserv.resolveApi(apiPort);
		TimestampSummary s;
		s = aserv.readSummary();
		// p("tss:"+s);

		String gapaIP = appIP;
		int gapaPort = 2001;
		try {
			apiid = api.login(groupId, username, passwd, gapaIP, gapaPort, appIP,
					appPort, aserv.readSummary());
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// en aquest punt encara pot no haber arribat el
		// newConnectedMemberEvent, s'hauria de bloquejar..
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		} // esperem rebre l'event
		memberId = ((ApiImpl) aserv.getASAI()).getMemberId();
		p("Membre conectat, el meu id és : " + memberId);
		p("Numero de nodes presents: "+aserv.getNumMembers());
	//	dades = new MetaDades(defpath, api, aserv, apiid);
		org = new Organitzador(api, aserv, apiid);
		assertTrue(true);

		
		
		
		
		
		
		
		
		String path = "/hola";
		
		int i = org.ModificarDir(path);
		org.Fet(i);
		int j = org.LlegirDir(path);
		
		org.Fet(j);

		try {
			api.logout(groupId, memberId, apiid);
			aserv.unpublish();
			aserv.close();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
